package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.model.FoodType;
import com.service.FoodTypeService;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("*")
public class FoodTypeController {

    @Autowired
    private FoodTypeService foodTypeService;

    /**
     * 添加菜品类型接口
     */
    @PostMapping("/foodTypes")
    public Result addFoodType(@RequestBody FoodType foodType) {
        boolean result = foodTypeService.save(foodType);
        if (result){
            return new Result("200","添加成功",1,null);
        } else {
            return new Result("500","添加失败,请联系管理员询问原因",0,null);
        }
    }

    /**
     * 根据ID删除菜品类型接口
     */
    @DeleteMapping("/foodTypes/{id}")
    public Result deleteFoodTypeById(@PathVariable Integer id) {
        boolean result = foodTypeService.removeById(id);
        if (result){
            return new Result("200","删除成功",1,null);
        } else {
            return new Result("500","删除失败",0,null);
        }
    }

    /**
     * 根据食品类型ID 查询食品类型
     * @param id 食品类型ID
     * @return 返回查询到的食品类型对象
     */
    @GetMapping("/foodTypes/{id}")
    public Result getFoodTypeById(@PathVariable("id") Integer id) {
        FoodType foodType = foodTypeService.getOne(new QueryWrapper<FoodType>().eq("id", id));
        return new Result("200","查询成功",1, List.of(foodType));
    }

    /**
     * 根据ID或名称查询单个菜品类型接口
     */
    /*@GetMapping("/foodTypes/{idOrTypeName}")
    public Result getFoodTypeByIdOrName(@PathVariable String idOrTypeName) {
        QueryWrapper<FoodType> wrapper = new QueryWrapper<>();
        wrapper.and(i -> i.eq("id", idOrTypeName)
                .or()
                .eq("type_name", idOrTypeName));
        FoodType foodType = foodTypeService.getOne(wrapper);
        return new Result("200","查询成功",1, List.of(foodType));
    }*/

    /**
     * 查询全部菜品类型接口
     */
    @GetMapping("/foodTypes")
    public Result getAllFoodTypes() {
        List<FoodType> list = foodTypeService.list();
        return new Result("200","查询成功",list.size(),list);
    }

    /**
     * 分页查询菜品类型接口
     */
    @GetMapping("/foodTypes/page/{pageNum}/{pageSize}")
    public IPage<FoodType> getFoodTypesByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return foodTypeService.page(new Page<>(pageNum, pageSize), null);
    }

    /**
     * 根据ID更新菜品类型接口
     */
    @PutMapping("/foodTypes/{id}")
    public Result updateFoodTypeById(@PathVariable Integer id, @RequestBody FoodType foodType) {
        foodType.setId(id);
        boolean result = foodTypeService.updateById(foodType);
        if (result){
            return new Result("200","更新成功",1,List.of(foodType));
        } else {
            return new Result("500","更新失败",0,null);
        }
    }

}

